import os
import xlwings as xw
import time

start = time.time()
with xw.App(visible=False) as app:
    fails = os.listdir('./')
    if fails is None:
        print('没有查找到文件')
    else:
        print(fails)
        # num = int(input('从零开始，请问您要加密第一个文件，请填入阿拉伯数字'))
        for fail in fails:

            if 'jiami' in fail and fail.endswith('xlsx'):
                # fail_path = os.path.abspath(fail)
                wb = app.books.open(fail)
                pas = input('请输入你的加密密码')
                # path = os.rename(fail, fail + str(f'{pas}'))
                file_path = os.path.splitext(fail)
                # fail2 = str(file_path[0] + pas + file_path[-1])
                fail1 = input('请输入新文件名')
                fail2 = str(fail1 + file_path[-1])
                wb.save(fail2, password=pas)
                os.remove(fail)
                print(f'已为您的文件-----{fail}加密，文件已经重新生成{fail2}密码为{pas},请牢记')

print(f'程序运行时间为{round(time.time() - start, 2)}秒')
